﻿using System.Linq;
using System.Threading.Tasks;

namespace Sidvall.AuthorizationManager.UI.Entities
{
	public sealed class OverView : global::Sidvall.AuthorizationManager.UI.Entities.Core.BusinessBase
	{
		#region Public Members

		#region AuthorizationScopeCount

		private long _AuthorizationScopeCount = 0;
		public long AuthorizationScopeCount
		{
			get
			{
				return _AuthorizationScopeCount;
			}
			set
			{
				_AuthorizationScopeCount = value;
				OnPropertyChanged("AuthorizationScopeCount");
			}
		}

		#endregion

		#region ReloadAsync

		public async Task ReloadAsync()
		{
			System.Collections.Generic.List<System.Threading.Tasks.Task> tasks;
			System.Collections.ObjectModel.Collection<DataFetchResult> dataFetchResults;
			Sidvall.AuthorizationManager.Net.Services.V1.ServerConnection serverConnection;
			DataFetchResult dataFetchResult;
			long applicationCount;
			int iMax;

			// Prepare tasks
			dataFetchResults = new System.Collections.ObjectModel.Collection<DataFetchResult>();
			foreach (var serverManager in Sidvall.AuthorizationManager.UI.SystemContext.Current.ServerManagers.ValidItems)
			{
				serverConnection = (from o in Sidvall.AuthorizationManager.SystemContext.Current.UserConfiguration.ServerConnections.Items
									where o.ServerConnectionId == serverManager.ServerConnectionId
									select o).FirstOrDefault();
				dataFetchResult = new DataFetchResult()
				{
					ServerManager = serverManager,
					ServerConnection = serverConnection,
				};
				dataFetchResults.Add(dataFetchResult);
			}

			// Create tasks
			tasks = new System.Collections.Generic.List<System.Threading.Tasks.Task>();
			iMax = dataFetchResults.Count;
			for (int i = 0; i < iMax; i++)
			{
				tasks.Add(ReloadAsync(dataFetchResults, i));
			}
			if (tasks.Count > 0)
				await System.Threading.Tasks.Task.WhenAll(tasks);

			// Reload items
			applicationCount = 0;
			iMax = dataFetchResults.Count;
			for (int i = 0; i < iMax; i++)
			{
				dataFetchResult = dataFetchResults[i];
				if (dataFetchResult.Exception != null)
					continue;
				applicationCount += dataFetchResult.AuthorizationScopes.Count;
			}
			this.AuthorizationScopeCount = applicationCount;
		}
		private async Task ReloadAsync(System.Collections.ObjectModel.Collection<DataFetchResult> dataFetchResults, int index)
		{
			Sidvall.Data.FetchListCriteria<Data.Fields.AuthorizationScopeField, Data.Relations.AuthorizationScopeRelation> criteria;
			DataFetchResult dataFetchResult;

			dataFetchResult = dataFetchResults[index];
			try
			{
				criteria = ParameterManager.GetAuthorizationScopeFetchListCriteria(dataFetchResult.ServerConnection.ServerConnectionId.Value);
				dataFetchResult.AuthorizationScopes = await Sidvall.AuthorizationManager.UI.SystemContext.Current.DataPortal.AuthorizationScopeManager.GetItemsAsync(criteria);
				dataFetchResult.Exception = null;
			}
			catch (System.Exception ex)
			{
				dataFetchResult.Exception = ex;
			}
		}

		#endregion

		#endregion
		#region Private Members

		private const string FullNameSeparator = "/";

		#endregion
		#region Constructors

		public OverView()
		{
		}

		public OverView(Sidvall.Data.DataRowState? dataRowState)
			: base(dataRowState)
		{
		}

		#endregion
	}
}